//
//  ThreadData.m
//
//  Created by lianchuang  on 14-11-28
//  Copyright (c) 2014 __MyCompanyName__. All rights reserved.
//

#import "ThreadData.h"


NSString *const kThreadDataDisplayorder = @"displayorder";
NSString *const kThreadDataSubject = @"subject";
NSString *const kThreadDataTypeid = @"typeid";
NSString *const kThreadDataDigest = @"digest";
NSString *const kThreadDataClosed = @"closed";
NSString *const kThreadDataAuthor = @"author";
NSString *const kThreadDataLastpost = @"lastpost";
NSString *const kThreadDataAuthorid = @"authorid";
NSString *const kThreadDataHighlight = @"highlight";
NSString *const kThreadDataPic = @"pic";
NSString *const kThreadDataGid = @"gid";
NSString *const kThreadDataGood = @"good";
NSString *const kThreadDataSummary = @"summary";
NSString *const kThreadDataDateline = @"dateline";
NSString *const kThreadDataReplies = @"replies";
NSString *const kThreadDataTid = @"tid";
NSString *const kThreadDataCollection = @"collection";
NSString *const kThreadDataViews = @"views";


@interface ThreadData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ThreadData

@synthesize displayorder = _displayorder;
@synthesize subject = _subject;
@synthesize typeid = _typeid;
@synthesize digest = _digest;
@synthesize closed = _closed;
@synthesize author = _author;
@synthesize lastpost = _lastpost;
@synthesize authorid = _authorid;
@synthesize highlight = _highlight;
@synthesize pic = _pic;
@synthesize gid = _gid;
@synthesize good = _good;
@synthesize summary = _summary;
@synthesize dateline = _dateline;
@synthesize replies = _replies;
@synthesize tid = _tid;
@synthesize collection = _collection;
@synthesize views = _views;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.displayorder = [self objectOrNilForKey:kThreadDataDisplayorder fromDictionary:dict];
            self.subject = [self objectOrNilForKey:kThreadDataSubject fromDictionary:dict];
            self.typeid = [self objectOrNilForKey:kThreadDataTypeid fromDictionary:dict];
            self.digest = [self objectOrNilForKey:kThreadDataDigest fromDictionary:dict];
            self.closed = [self objectOrNilForKey:kThreadDataClosed fromDictionary:dict];
            self.author = [self objectOrNilForKey:kThreadDataAuthor fromDictionary:dict];
            self.lastpost = [self objectOrNilForKey:kThreadDataLastpost fromDictionary:dict];
            self.authorid = [self objectOrNilForKey:kThreadDataAuthorid fromDictionary:dict];
            self.highlight = [self objectOrNilForKey:kThreadDataHighlight fromDictionary:dict];
            self.pic = [self objectOrNilForKey:kThreadDataPic fromDictionary:dict];
            self.gid = [self objectOrNilForKey:kThreadDataGid fromDictionary:dict];
            self.good = [self objectOrNilForKey:kThreadDataGood fromDictionary:dict];
            self.summary = [self objectOrNilForKey:kThreadDataSummary fromDictionary:dict];
            self.dateline = [self objectOrNilForKey:kThreadDataDateline fromDictionary:dict];
            self.replies = [self objectOrNilForKey:kThreadDataReplies fromDictionary:dict];
            self.tid = [self objectOrNilForKey:kThreadDataTid fromDictionary:dict];
            self.collection = [[self objectOrNilForKey:kThreadDataCollection fromDictionary:dict] doubleValue];
            self.views = [self objectOrNilForKey:kThreadDataViews fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.displayorder forKey:kThreadDataDisplayorder];
    [mutableDict setValue:self.subject forKey:kThreadDataSubject];
    [mutableDict setValue:self.typeid forKey:kThreadDataTypeid];
    [mutableDict setValue:self.digest forKey:kThreadDataDigest];
    [mutableDict setValue:self.closed forKey:kThreadDataClosed];
    [mutableDict setValue:self.author forKey:kThreadDataAuthor];
    [mutableDict setValue:self.lastpost forKey:kThreadDataLastpost];
    [mutableDict setValue:self.authorid forKey:kThreadDataAuthorid];
    [mutableDict setValue:self.highlight forKey:kThreadDataHighlight];
    NSMutableArray *tempArrayForPic = [NSMutableArray array];
    for (NSObject *subArrayObject in self.pic) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForPic addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForPic addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForPic] forKey:kThreadDataPic];
    [mutableDict setValue:self.gid forKey:kThreadDataGid];
    [mutableDict setValue:self.good forKey:kThreadDataGood];
    [mutableDict setValue:self.summary forKey:kThreadDataSummary];
    [mutableDict setValue:self.dateline forKey:kThreadDataDateline];
    [mutableDict setValue:self.replies forKey:kThreadDataReplies];
    [mutableDict setValue:self.tid forKey:kThreadDataTid];
    [mutableDict setValue:[NSNumber numberWithDouble:self.collection] forKey:kThreadDataCollection];
    [mutableDict setValue:self.views forKey:kThreadDataViews];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.displayorder = [aDecoder decodeObjectForKey:kThreadDataDisplayorder];
    self.subject = [aDecoder decodeObjectForKey:kThreadDataSubject];
    self.typeid = [aDecoder decodeObjectForKey:kThreadDataTypeid];
    self.digest = [aDecoder decodeObjectForKey:kThreadDataDigest];
    self.closed = [aDecoder decodeObjectForKey:kThreadDataClosed];
    self.author = [aDecoder decodeObjectForKey:kThreadDataAuthor];
    self.lastpost = [aDecoder decodeObjectForKey:kThreadDataLastpost];
    self.authorid = [aDecoder decodeObjectForKey:kThreadDataAuthorid];
    self.highlight = [aDecoder decodeObjectForKey:kThreadDataHighlight];
    self.pic = [aDecoder decodeObjectForKey:kThreadDataPic];
    self.gid = [aDecoder decodeObjectForKey:kThreadDataGid];
    self.good = [aDecoder decodeObjectForKey:kThreadDataGood];
    self.summary = [aDecoder decodeObjectForKey:kThreadDataSummary];
    self.dateline = [aDecoder decodeObjectForKey:kThreadDataDateline];
    self.replies = [aDecoder decodeObjectForKey:kThreadDataReplies];
    self.tid = [aDecoder decodeObjectForKey:kThreadDataTid];
    self.collection = [aDecoder decodeDoubleForKey:kThreadDataCollection];
    self.views = [aDecoder decodeObjectForKey:kThreadDataViews];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_displayorder forKey:kThreadDataDisplayorder];
    [aCoder encodeObject:_subject forKey:kThreadDataSubject];
    [aCoder encodeObject:_typeid forKey:kThreadDataTypeid];
    [aCoder encodeObject:_digest forKey:kThreadDataDigest];
    [aCoder encodeObject:_closed forKey:kThreadDataClosed];
    [aCoder encodeObject:_author forKey:kThreadDataAuthor];
    [aCoder encodeObject:_lastpost forKey:kThreadDataLastpost];
    [aCoder encodeObject:_authorid forKey:kThreadDataAuthorid];
    [aCoder encodeObject:_highlight forKey:kThreadDataHighlight];
    [aCoder encodeObject:_pic forKey:kThreadDataPic];
    [aCoder encodeObject:_gid forKey:kThreadDataGid];
    [aCoder encodeObject:_good forKey:kThreadDataGood];
    [aCoder encodeObject:_summary forKey:kThreadDataSummary];
    [aCoder encodeObject:_dateline forKey:kThreadDataDateline];
    [aCoder encodeObject:_replies forKey:kThreadDataReplies];
    [aCoder encodeObject:_tid forKey:kThreadDataTid];
    [aCoder encodeDouble:_collection forKey:kThreadDataCollection];
    [aCoder encodeObject:_views forKey:kThreadDataViews];
}

- (id)copyWithZone:(NSZone *)zone
{
    ThreadData *copy = [[ThreadData alloc] init];
    
    if (copy) {

        copy.displayorder = [self.displayorder copyWithZone:zone];
        copy.subject = [self.subject copyWithZone:zone];
        copy.typeid = [self.typeid copyWithZone:zone];
        copy.digest = [self.digest copyWithZone:zone];
        copy.closed = [self.closed copyWithZone:zone];
        copy.author = [self.author copyWithZone:zone];
        copy.lastpost = [self.lastpost copyWithZone:zone];
        copy.authorid = [self.authorid copyWithZone:zone];
        copy.highlight = [self.highlight copyWithZone:zone];
        copy.pic = [self.pic copyWithZone:zone];
        copy.gid = [self.gid copyWithZone:zone];
        copy.good = [self.good copyWithZone:zone];
        copy.summary = [self.summary copyWithZone:zone];
        copy.dateline = [self.dateline copyWithZone:zone];
        copy.replies = [self.replies copyWithZone:zone];
        copy.tid = [self.tid copyWithZone:zone];
        copy.collection = self.collection;
        copy.views = [self.views copyWithZone:zone];
    }
    
    return copy;
}


@end
